package org.example.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.common.JwtHelper;
import org.example.common.Result;
import org.example.common.ResultCodeEnum;
import org.example.pojo.Category;
import org.example.pojo.Query;
import org.example.pojo.User;
import org.example.service.UserService;
import org.example.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
* @author 13326
* @description 针对表【hards_user】的数据库操作Service实现
* @createDate 2025-01-06 11:12:33
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
@Resource
    private UserMapper userMapper;
    @Autowired
    private JwtHelper jwtHelper;
    @Override
public Result login(User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        Long count=userMapper.selectCount(lambdaQueryWrapper);
        if(count==0){
            return Result.build(501, ResultCodeEnum.USERNAME_USED);
        }
        User user1=userMapper.selectOne(lambdaQueryWrapper);
        if(!user1.getPassword().equals(user.getPassword())){
            return Result.build(503,ResultCodeEnum.PASSWORD_ERROR);
        }
        String token=jwtHelper.createToken(Long.valueOf(user1.getId()));
        HashMap<String,Object> map=new HashMap<>();
        map.put("token",token);
        map.put("user",user1);
        return Result.ok(map);


    }
    @Override
    public Result registry(User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        Long count=userMapper.selectCount(lambdaQueryWrapper);
        if(count>0){
            return Result.build(503,ResultCodeEnum.USERNAME_USED);

        }
        int rows=userMapper.insert(user);
        return Result.ok("成功");

    }
    @Override
    public Result getlist(Query query){
        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper
                .like(!StringUtils.isEmpty(query.getKeyword()), User::getUsername,query.getKeyword())
                .eq(User::getUsername,query.getKeyword());
        IPage<User> page=new Page<>(query.getPageNum(),query.getPageSize());
        userMapper.selectPageMap(page,query);
        HashMap<String,Object> hashMap=new HashMap<>();
        hashMap.put("total",page.getTotal());
        hashMap.put("rows",page.getRecords());
        hashMap.put("pages",page.getPages());
        hashMap.put("size",page.getSize());
        return Result.ok(hashMap);


    }
    @Override
    public Result add(User user){
        LambdaQueryWrapper<User> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(User::getUsername,user.getUsername());
        Long count=userMapper.selectCount(lambdaQueryWrapper);
        if(count>0){
            return Result.build(503,ResultCodeEnum.USERNAME_USED);
        }
        userMapper.insert(user);
        return Result.ok("新增成功");

    }
    @Override
    public Result updateuser(User user){
        userMapper.updateById(user);
        return Result.ok("修改成功");
    }
    @Override
    public Result delete(Integer id){
        userMapper.deleteById(id);
        return Result.ok("删除成功");
    }

}




